﻿#include <iostream>
#include <vector>
using namespace std;


class Solution 
{
public:
    // 215. 数组中的第K个最大元素
    int findKthLargest(vector<int>& nums, int k)
    {
        srand(time(nullptr));
        return qsort(nums, 0, nums.size() - 1, k);
    }

    int qsort(vector<int>& nums, int l, int r, int k)
    {
        if (l >= r)
            return nums[l];

        int key = GetKey(nums, l, r);
        int left = l - 1, i = l, right = r + 1;
        while (i < right)
        {
            if (nums[i] < key)
                swap(nums[++left], nums[i++]);
            else if (nums[i] == key)
                i++;
            else
                swap(nums[--right], nums[i]);
        }

        int c = r - right + 1, b = right - left - 1;
        if (c >= k)
            return qsort(nums, right, r, k);
        else if (b + c >= k)
            return key;
        else
            return qsort(nums, l, left, k - b - c);
    }

    int GetKey(vector<int>& nums, int left, int right)
    {
        return nums[rand() % (right - left + 1) + left];
    }
};

int main()
{

    return 0;
}